home *** CD-ROM | disk | FTP | other *** search
- #ifndef LINT
- static char sccsid[]="@(#) misc2.c 1.8 87/05/29 12:54:33";
- #endif /* LINT */
-
- /*
- Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
- */
- #include "options.h"
- /* Miscellaneous routines needed by both Zoo and Ooz */
- #ifdef NOFCNTL
- #include <file.h>
- #else
- #include <fcntl.h>
- #endif
-
- #ifdef FLAT
- #include <types.h>
- #include <stat.h>
- #else
- #include <sys/types.h>
- #include <sys/stat.h>
- #endif
-
- #include "portable.h"
- #include <stdio.h>
- #include "various.h"
- #include "zoofns.h" /* only for malloc */
- #include "errors.i"
- #include "zoomem.h"
- #include "zoo.h"
-
- /**********************/
- /* memerr() */
- /* Give error message on memory error and abort */
- void memerr()
- {
- #ifdef OOZ
- prterror ('f', no_memory, "", "");
- #else
- prterror ('f', no_memory);
- #endif
- }
-
- /**********************/
- /*
- emalloc() allocates memory like malloc() does, except that it automatically
- calls the error function memerr() if memory couldn't be allocated. It also
- assumes (unless small memory allocation is being done) that memory will
- never be freed and conserves it by allocating memory in large chunks
- and then partitioning it out with no administrative overhead.
- */
-
- char *emalloc (size)
- unsigned int size;
- {
- #define BLOCK_SIZE 512 /* memory allocation granularity */
-
- #ifdef USE_MALLOC
- /* Pass on memory requests to malloc() */
- char *ptr;
- if ((ptr = malloc (size)) == NULL)
- memerr();
- return (ptr);
- #else
- static char *memptr;
- static unsigned avail = 0;
- unsigned malloc_incr;
- char *retval;
-
- if (size == 0)
- return (NULL);
-
- /* if not enough space avail get some more */
- if (avail < size) {
- malloc_incr = BLOCK_SIZE;
- if (malloc_incr < size)
- malloc_incr = size;
- while (malloc_incr >= size && (memptr = malloc (malloc_incr)) == NULL)
- malloc_incr = (malloc_incr / 6) * 5;
- avail = malloc_incr;
- }
-
- if (avail < size)
- memerr();
- else {
- retval = memptr;
- memptr += size;
- avail -= size;
- return (retval);
- }
- #endif /* end of not USE_MALLOC */
- }
-
- /**********************/
- /* putstr()
- This function prints a string to standard output without using printf().
- If a null string, nothing is printed (not even the null character).
- */
- putstr (str)
- register char *str;
- {
- if (str == NULL)
- return;
- while (*str) {
- fputchar (*str++);
- }
- }
-
- /**********************/
- /* exists()
- This function checks the existence of a file.
-
- If the symbol EXISTS is defined, that is called as a macro and
- supplied the filename. It must return 1 if the file exists and
- 0 if it does not.
-
- If EXISTS is not defined, exists() tests to see if the file can be
- opened for either reading or writing; if so, it returns 1 else it
- returns 0.
-
- Because of the delay between the time existence is checked and the time Zoo
- creates a files, a race condition exists. It would be better to
- use open() with the O_EXCL flag but that will not work for many
- systems.
- */
-
- int exists (fname)
- char *fname;
- {
- #ifdef EXISTS
- return EXISTS(fname);
- #else
- int han;
-
- #ifdef PORTABLE
- if ((han = OPEN(fname,F_READ)) != -1 || (han = OPEN(fname,F_WRITE)) != -1)
- #else
- /* Under MS-DOS, all files are readable */
- if ((han = OPEN(fname,F_READ)) != -1)
- #endif /* ifdef PORTABLE */
- {
- close (han);
- return (1);
- } else
- return (0);
- #endif /* ifdef EXISTS */
- }
-
- /************************************************************************
- The following MS-DOS-specific functions read directory entries and zoo
- headers. Portable versions are elsewhere.
-
- Note: At some future time, it will be better to make the MS-DOS versions
- of these into macros.
- */
-
- /* Currently using portable I/O for MSC too */
- #ifdef COMMENT
- #ifndef PORTABLE
- /**********************
- Function frd_zooh() reads the header of a Zoo archive from a FILE.
- */
- int frd_zooh(zoo_header, zoo_file)
- struct zoo_header *zoo_header;
- FILE *zoo_file;
- {
- if (fread ((char *) zoo_header, SIZ_ZOOH, 1, zoo_file) != 1)
- return (-1);
- else
- return (0);
- }
-
- /**********************
- Function frd_dir() reads a directory entry from a FILE.
- */
- int frd_dir(direntry, zoo_file)
- struct direntry *direntry;
- FILE *zoo_file;
- {
- if (fread ((char *) direntry, SIZ_DIR, 1, zoo_file) != 1)
- return (-1);
- else
- return (0);
- }
-
- /**********************
- Function rd_zooh() reads a Zoo archive header from a file handle.
- */
- int rd_zooh (header, zoo_han)
- struct zoo_header *header;
- int zoo_han;
- {
- return (read (zoo_han, (char *) header, SIZ_ZOOH));
- }
-
- /**********************
- Function rd_dir() reads a directory entry from a file handle */
- int rd_dir(direntry, zoo_han)
- struct direntry *direntry;
- int zoo_han;
- {
- return (read (zoo_han, (char *) direntry, SIZ_DIR));
- }
-
- #endif /* end of not PORTABLE */
- #endif /* COMMENT */
-
- /****************
- newcat() allocates enough space to concatenate two strings then returns
- a pointer to the concatenated result */
-
- char *newcat (r, s)
- char *r, *s;
- {
- char *temp = emalloc (strlen (r) + strlen (s) + 2); /* 1 spare */
- strcpy (temp, r);
- strcat (temp, s);
- return (temp);
- }
-
-
- /* Creates a path */
- int makepath(path)
- char *path;
- {
- char tmppath[PATHSIZE];
- char *slashpos;
- if (path == NULL)
- return;
- while (*lastptr(path) == *PATH_CH) /* remove trailing slashes */
- *lastptr(path) = '\0';
- if (*path == '\0')
- return;
-
- slashpos = findlast(path, PATH_CH); /* find last slash */
- if (slashpos == NULL) { /* if not, just create dir. */
- MKDIR(path);
- return;
- } else { /* otherwise... */
- if (slashpos == path) { /* if leading slash */
- MKDIR(slashpos); /* make that directory */
- return; /* and done */
- } else {
- strcpy(tmppath,path); /* save path */
- *slashpos = '\0'; /* split into prefix & suffix */
- #ifdef DEBUG
- printf("making path from [%s]\n", path);
- #endif
- makepath(path); /* make path from prefix */
- #ifdef DEBUG
- printf("making dir from [%s]\n", tmppath);
- #endif
- MKDIR(tmppath); /* make dir from suffix */
- }
- }
- } /* makepath() */
-
- /*
- If no extension in filename add supplied extension
- */
- char *addext (fname, ext)
- char *fname;
- char *ext;
- {
- if (strchr (nameptr (fname), EXT_CH) == NULL)
- return (newcat (fname, ext));
- else
- return (fname);
- }
-